package com.ag.hmk.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.Resource;
import com.ag.admin.service.impl.UserServiceImpl;
import com.ag.bean.TbExpert;
import com.ag.bean.TbExpertArea;
import com.ag.bean.TbType;
import com.ag.bean.TbUser;
import com.ag.hmk.service.impl.ExpertAreaServImpl;
import com.ag.hmk.service.impl.ExpertServImpl;
import com.ag.hmk.service.impl.WukuTypeServImpl;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @author 黄木看
 * 新增专家记录action
 */
public class SaveExpertAction extends ActionSupport {
	private static final long serialVersionUID = 5164783527991202002L;
	private ExpertServImpl expertServImpl;
	private WukuTypeServImpl wukuTypeServImpl;
	private ExpertAreaServImpl expertAreaServImpl;
	
	@Resource(name = "userServiceImpl")
	private UserServiceImpl userService;
	private String selDepts;
	private String leadDepts;
	
	
	private String expertID1;
	private String type1ID;
	private String area1ID;
	private String expName1;
	private String expBirthday1;
	private String expSex1;
	private String expDegree1;
	private String expEdut1;
	private String expImg1;
	private String expPost1;
	private String expRusume1;
	private String expSchool1;
	private String expTitle1;
	private String expUnit1;
	private String expPro1;
	private String expPhone1;
	
	private String fileID1;
	private String fileNameID1;
	
	private String expPwd;
	private String expFax;
	private String expTel;
	private String expEmail;
	private String expStatusID;
	
	private String ajaxRST;
	/*
	 * 保存专家记录
	 */
	public String saveExpert()throws Exception{
		this.setAjaxRST("0");
		try {
			TbType type1 = wukuTypeServImpl.findTypeByID(type1ID);
			TbExpertArea expertArea1 = expertAreaServImpl.findByExpAreaID(area1ID);
			if (type1!=null && expertArea1!=null) {
				if (expertID1!=null && expertID1.length()>0 && expName1!=null && expName1.length()>0 && 
					//expBirthday1!=null && expBirthday1.length()>0 &&  (去掉日期必选)
					expSex1!=null && expSex1.length()>0 
					) {
						TbUser user1 = null; //new TbUser();
						TbExpert expert = new TbExpert();
						expert.setExpId(expertID1);
						expert.setTbUser(userService.findById(expertID1));
						expert.setTbType(type1);
						expert.setTbExpertArea(expertArea1);
						expert.setExpName(expName1);
						SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						Date birDate = null;
						if(expBirthday1!=null){
							try {
								birDate = dateFormat.parse(expBirthday1);
								System.out.println("-----birdate:"+birDate);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						expert.setExpBirthday(birDate);
						expert.setExpSex(expSex1);
						expert.setExpDeree(expDegree1);
						expert.setExpEdutcation(expEdut1);
						expert.setExpImg(fileID1);
						expert.setC(fileNameID1); //存放文件的真实文件名
						expert.setExpPost(expPost1);
						expert.setExpRusume(expRusume1);
						expert.setExpSchool(expSchool1);
						expert.setExpTitle(expTitle1);
						expert.setExpUnit(expUnit1);
						expert.setExtProfession(expPro1);
						expert.setExpMobile(expPhone1);
						expert.setExpFax(expFax);
						expert.setExpTel(expTel);
						
						expertServImpl.saveExpert(expert);
						
						this.ajaxRST="1";
						System.out.println("===============save expert ok!");
					
					
				} else {
					System.out.println("============can not to be null");
					this.setAjaxRST("0");
				}
				
			} else {
				System.out.println("type or expertArea cannot be null!");
				this.setAjaxRST("0");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		
		return "saveExpertOK";
	}
	
	
	/*
	 * 更新专家记录
	 */
	public String updateExpert()throws Exception{
		this.setAjaxRST("0");
		try {
			TbType type1 = wukuTypeServImpl.findTypeByID(type1ID);
			TbExpertArea expertArea1 = expertAreaServImpl.findByExpAreaID(area1ID);
			if (type1!=null && expertArea1!=null) {
				if (expertID1!=null && expertID1.length()>0 && expName1!=null && expName1.length()>0 && 
					expBirthday1!=null && expBirthday1.length()>0 && 
					expSex1!=null && expSex1.length()>0  ) {
						TbUser user = userService.findById(expertID1);
						TbExpert expert = expertServImpl.findByExpertID(expertID1);
						if (expert!=null) {
							expert.setTbType(type1);
							expert.setTbExpertArea(expertArea1);
							expert.setTbUser(user);
							expert.setExpName(expName1);
							SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
							Date birDate = null;
							try {
								birDate = dateFormat.parse(expBirthday1);
								System.out.println("-----birdate:"+birDate);
							} catch (Exception e) {
								e.printStackTrace();
							}
							expert.setExpBirthday(birDate);
							expert.setExpSex(expSex1);
							expert.setExpDeree(expDegree1);
							expert.setExpEdutcation(expEdut1);
							expert.setExpImg(fileID1);
							expert.setC(fileNameID1); //存放文件的真实文件名
							expert.setExpPost(expPost1);
							expert.setExpRusume(expRusume1);
							expert.setExpSchool(expSchool1);
							expert.setExpTitle(expTitle1);
							expert.setExpUnit(expUnit1);
							expert.setExtProfession(expPro1);
							expert.setExpMobile(expPhone1);
							expert.setExpFax(expFax);
							expert.setExpTel(expTel);
							
							expertServImpl.saveExpert(expert);
							this.ajaxRST="1";
							System.out.println("更新成功!");
					}
				}
			}
						
		} catch (Exception e) {
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		
		return "updateExpertOK";
	}
	
	
	public void setExpertServImpl(ExpertServImpl expertServImpl) {
		this.expertServImpl = expertServImpl;
	}
	public void setWukuTypeServImpl(WukuTypeServImpl wukuTypeServImpl) {
		this.wukuTypeServImpl = wukuTypeServImpl;
	}
	public void setExpertAreaServImpl(ExpertAreaServImpl expertAreaServImpl) {
		this.expertAreaServImpl = expertAreaServImpl;
	}


	public void setType1ID(String type1ID) {
		this.type1ID = type1ID;
	}


	public void setArea1ID(String area1ID) {
		this.area1ID = area1ID;
	}


	public void setExpName1(String expName1) {
		this.expName1 = expName1;
	}


	public void setExpBirthday1(String expBirthday1) {
		this.expBirthday1 = expBirthday1;
	}


	public void setExpSex1(String expSex1) {
		this.expSex1 = expSex1;
	}


	public void setExpDegree1(String expDegree1) {
		this.expDegree1 = expDegree1;
	}


	public void setExpEdut1(String expEdut1) {
		this.expEdut1 = expEdut1;
	}


	public void setExpImg1(String expImg1) {
		this.expImg1 = expImg1;
	}


	public void setExpPost1(String expPost1) {
		this.expPost1 = expPost1;
	}


	public void setExpRusume1(String expRusume1) {
		this.expRusume1 = expRusume1;
	}


	public void setExpSchool1(String expSchool1) {
		this.expSchool1 = expSchool1;
	}


	public void setExpTitle1(String expTitle1) {
		this.expTitle1 = expTitle1;
	}


	public void setExpUnit1(String expUnit1) {
		this.expUnit1 = expUnit1;
	}


	public void setExpPro1(String expPro1) {
		this.expPro1 = expPro1;
	}


	public String getAjaxRST() {
		return ajaxRST;
	}


	public void setAjaxRST(String ajaxRST) {
		this.ajaxRST = ajaxRST;
	}


	public void setExpertID1(String expertID1) {
		this.expertID1 = expertID1;
	}


	public void setExpPhone1(String expPhone1) {
		this.expPhone1 = expPhone1;
	}


	public void setFileID1(String fileID1) {
		this.fileID1 = fileID1;
	}


	public void setSelDepts(String selDepts) {
		this.selDepts = selDepts;
	}


	public void setLeadDepts(String leadDepts) {
		this.leadDepts = leadDepts;
	}


	public void setExpPwd(String expPwd) {
		this.expPwd = expPwd;
	}


	public void setExpFax(String expFax) {
		this.expFax = expFax;
	}


	public void setExpEmail(String expEmail) {
		this.expEmail = expEmail;
	}


	public void setExpTel(String expTel) {
		this.expTel = expTel;
	}


	public void setExpStatusID(String expStatusID) {
		this.expStatusID = expStatusID;
	}


	public void setFileNameID1(String fileNameID1) {
		this.fileNameID1 = fileNameID1;
	}
	
}
